/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.beynet.docs.session;

import java.security.AccessController;
import java.security.Principal;
import javax.security.auth.Subject;
import org.beynet.docs.auth.GSyncPrincipal;

/**
 *
 * @author beynet
 */
public class SessionFactory {
    /**
     * return current Session from the Current Subject
     * @return
     */
    public static synchronized Session getCurrentSession() {
        if (session == null) {
            Subject subject = Subject.getSubject(AccessController.getContext());
            for (Principal p : subject.getPrincipals()) {
                if (p instanceof GSyncPrincipal) {
                    session = ((GSyncPrincipal) p).getSession();
                    break;
                }
            }
            if (session == null) {
                throw new RuntimeException("No authenticated");
            }
        }
        return (session);
    }
    
    private static Session session = null ;
}
